





#macth从头开始，，search所有中选一个，，findall取出所有
import re
#'^' 匹配字符开头，若指定flags MULTILINE，这种也可以匹配上
#(r"^a","\nabc\nee",flags=re.MULTILINE)
re.match('^Ou','OuYangSiJian123')
rem = re.match('^Ou','OuYangSiJian123')
print(rem)
print(rem.group())
rem = re.match('^Ou\d','Ou321YangSiJian123')
print(rem)
print(rem.group())
rem = re.match('^Ou\d+','Ou321YangSiJian123')
print(rem)
print(rem.group())
#"."  默认匹配除\n之外的任意一个字符，若指定flag DOTALL，则匹配任意字符，包括换行
rem = re.match('.','Ou321YangSiJian123')
print(rem)
rem = re.match('.+','Ou321YangSiJian123')
print(rem)

print("*****")
#"$"  匹配字符结尾，或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以
#"+"  匹配前一个字符1次或多次，re.findall("ab+","ab+cd+abb+bba"),结果为['ab','abb']
rem = re.match('O.+','Ou321YangSiJian123')
print(rem)
rem = re.match('3.+','Ou321YangSiJian123')
print(rem)
rem = re.search('Y.+','Ou321YangSiJian123')
print(rem)
rem = re.search('Y.+n','Ou321YangSiJian123')
print(rem)
rem = re.search('Y.+a$','Ou321YangSiJian123a')
print(rem)
rem = re.search('Y[a-z]','Ou321YangSiJian123a')
print(rem)
rem = re.search('Y[a-z]+j','Ou321YangbghjSiJian123a')
print(rem)
rem = re.search('Y[a-z]+1','Ou321YangbghjSiJian123a')
print(rem)
rem = re.search('Y[a-zA-Z]+Z','Ou321YangbghjSiJianZ123a')
print(rem)
rem = re.search('Y[a-zA-Z0-9]+1','Ou321YangbghjSiJianZ123a')
print(rem)
rem = re.search('#.+#','123#hello##')
print(rem)
#"*"  匹配*号前的字符0次或多次，re.findall("ab*","cabb3abcbbac"),结果为["abb","ab",'a']


#"?"   匹配前一个字符1次或0次
rem = re.search('a?','abcd')#匹配a   0次或1次
print(rem)
rem = re.search('aa?','abcd')
print(rem)
rem = re.search('a?','bcd')
print(rem)
rem = re.search('aa?','abcd')
print(rem)
rem = re.search('aaaaaa?','aaaaabcd')
print(rem)
rem = re.search('aaaaaa?','aaaabcd')
print(rem)
rem = re.search('aaa?','abcd')
print(rem)
rem = re.search('aaa?','abcdaaa')
print(rem)
rem = re.search('aa?','bcdaa')
print(rem)
rem = re.search('aaa?','abcdaa')
print(rem)
rem = re.search('aaa?','aabcdaaa')
print(rem)
rem = re.search('aab?','aabcdaaa')
print(rem)
rem = re.search('aab?','aacdaaa')#b可有可无
print(rem)
#'{m}'   匹配前一个字符m次
res = re.search('[0-9]{3}','a1b256c3d4')
print(res)
res = re.search('[0-9]{3}','a781b256c3d4')
print(res)
res = re.findall('[0-9]{3}','a781b256c3d4')
print(res)
res = re.findall('[0-9]{3}','a1b256c3d4')
print(res)
#'{n,m}'   匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbb")结果为['abb','ab','abb']
res = re.search('[0-9]{1,3}','a1b256c3d4')
print(res)
res = re.search('[0-9]{3}','a1456b256c3d4')
print(res)
res = re.findall('[0-9]{1,3}','a1b256c3d4')
print(res)
#'|'  匹配|左或|右的字符，re.search("abc|ABC','ABCBabcCD').group()结果为'AB'
res = re.search("abc|ABC",'ABCBabcCD').group()
print(res)
res = re.findall("abc|ABC",'ABCBabcCD')
print(res)
#'(...)' 分组匹配,re.search("(abc){2}(123|456)c","abcabca456c").group()  结果为abcabca456c
res = re.search("abc{1}","alexabcc")#1代表c匹配一次
print(res)
res = re.search("abc{2}","alexabcc")
print(res)
res = re.search("(abc){2}","alexabcabc")
print(res)
res = re.search("(abc){2}\|","alexabcabc|")#\转义
print(res)
res = re.search("(abc){2}(\|\|\=)","alexabcabc||=")
print(res)

#'\A'  只从字符开头匹配，re.rearch("\Abc","alexabc")是匹配不到的
#'\Z'  匹配字符结尾，同￥
res = re.search("\A[0-9]+[a-z]\Z","123a")
print(res)


#'\d'  匹配数字0-9

#'\D'  匹配非数字
res = re.search("\D+","123a \n\t#$%*")
print(res)
#'\w'  匹配[A-Za-z0-9]
res = re.search("\w+","123abcAFT \n\t#$%*")
print(res)
#'\W'  匹配非[A-Za-z0-9]
res = re.search("\w+","123abcAFT \n\t#$%*")
print(res)
#'\s'  匹配空白字符，\t,\n,\r,re.search("\s+","ab\tc1\n3).group(),结果为"\t'
res = re.search("\s+","123\tabc\nAFT\r#$ %*")
print(res)
res = re.findall("\s+","123\tabc\nAFT\r#$ %     *")
print(res)
#'(?P<name>...)'  分组匹配re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})",'371481199306143242")
#结果为{'province':'3714','city':'81','birthday':'1933'}
res = re.search("(?P<id>[0-9]+)","abcd1234asd@#%54").group()
print(res)
res = re.search("(?P<id>[0-9]+)","abcd1234asd@#%54").groupdict()
print(res)
res = re.search("(?P<id>[0-9]+)(?P<name>[a-zA-Z]+)","abcd1234asd@#%54").groupdict()
print(res)
res = re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","371481199306143242").groupdict()
print(res)

#分割
res = re.split("[0-9]","abc12fjgj34FKK")
print(res)
res = re.split("[0-9]+","abc12fjgj34FKK")
print(res)
#匹配字符并替换
res = re.sub("[0-9]",'|',"abc12fjgj34FKK13#*")
print(res)
res = re.sub("[0-9]",'|',"abc12fjgj34FKK13#*",count=2)
print(res)

#反斜杠（有斜杠）
res = re.search(r"\\","abc12fjgj34FK\\K13#*")
print(res)
res = re.search(r"\\","abc12fjgj34FK\K13#*")
print(res)

#flags
res = re.search('[a-z]+','abjD',flags=re.I)#忽略大小写
print(res)
res = re.search('[a-z]+d$','abcjD\nsecondline\nthird',flags=re.M)
print(res)
res = re.search(r'^a','\nabc\neee',flags=re.M)
print(res)
res = re.search(r'^a','\nabc\neee')
print(res)
res = re.search(r'.+','\nabc\neee',flags=re.S)
print(res)